package com.software.frontend.mine.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.software.frontend.MainActivity;
import com.software.frontend.R;
import com.software.frontend.mine.utils.CameraRollManager;

import java.io.File;

public class LargeImageActivity extends AppCompatActivity {

    ImageView imageView, back;
    private Gson gson = new Gson();
    private String IMAGE_URL = "head";
    private Bitmap head;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_large_image);

        imageView = (ImageView) findViewById(R.id.large_image);
        back = (ImageView) findViewById(R.id.button_back);

        head = gson.fromJson(getIntent().getStringExtra("head"), Bitmap.class);
        imageView.setImageBitmap(head);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(imageView.getDrawable() instanceof BitmapDrawable){
                    Toast.makeText(getApplicationContext(),"长按保存图片至相册",Toast.LENGTH_SHORT).show();
                    File fileDir=new File(getApplication().getExternalCacheDir(),"images");
                    File file=new File(fileDir.getAbsolutePath()+"/"+head.hashCode()+".png");
                    if(file!=null&&file.length()>0){
                        CameraRollManager rollManager=new CameraRollManager(LargeImageActivity.this, Uri.parse(file.getAbsolutePath()));
                        rollManager.execute();
                    }
                }
                return false;
            }
        });
    }
}